﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FrmCount : System.Windows.Forms.Form
	{
		public FrmCount()
		{
			this.InitializeComponent();
		}

		public FrmCount(ExternalCommandData cmdData, string msg, ElementSet elements)
		{
			this.InitializeComponent();
			this.cmdDataForm = cmdData;
			this.msgForm = msg;
			this.elementsForm = elements;
			this.BindPro();
			this.BindFileName();
		}

		private void BindFileName()
		{
			this.sPath = Path.GetDirectoryName(this.sPath);
			this.sPath = Path.GetDirectoryName(this.sPath);
			this.sFileName = this.sPath + "\\Data\\SelSet.xml";
		}

		private void BindPro()
		{
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				return;
			}
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (activeUIDocument.Document.Title != "")
			{
				this.proTitle = activeUIDocument.Document.Title;
			}
			if (activeUIDocument.Document.PathName != "")
			{
				this.proPath = activeUIDocument.Document.PathName;
			}
			XmlInfo.RegForm(this.proTitle, this.proPath);
		}

		private void FrmCount_Move(object sender, EventArgs e)
		{
			System.Drawing.Point point = base.PointToScreen(this.panel1.Location);
			this.frmCountSel.Top = point.Y;
			this.frmCountSel.Left = point.X;
			point = base.PointToScreen(this.panel2.Location);
			this.frmCountSpec.Top = point.Y;
			this.frmCountSpec.Left = point.X;
			point = base.PointToScreen(this.panel3.Location);
			this.frmCountMater.Top = point.Y;
			this.frmCountMater.Left = point.X;
		}

		private void FrmCount_Load(object sender, EventArgs e)
		{
			string selSetStr = XmlInfo.GetSelSetStr(this.sFileName, this.proPath, this.proTitle);
			if (selSetStr == "" || selSetStr == null)
			{
				this.ShowFirst(false);
				return;
			}
			this.ShowFirst(true);
			XmlInfo.AddSelSet(this.proPath, this.proTitle, selSetStr);
			this.LoadFirst();
			this.textBox1.Focus();
			XmlInfo.SetCountMater(RevitTools.CountGetPipeMater(this.cmdDataForm));
			this.BindFirstMater();
			XmlInfo.SetCountSpec(RevitTools.CountGetPipeSpec(this.cmdDataForm));
			this.BindFirstSpec();
		}

		private void ShowFirst(bool b)
		{
			this.chkFirst.Visible = b;
			this.txtFirst.Visible = b;
			this.btnFirstShow.Visible = b;
			this.btnFirstLock.Visible = b;
			this.btnDropDown.Visible = b;
			this.txtInfo.Visible = !b;
			this.textBox1.Focus();
		}

		public void LoadFirst()
		{
			try
			{
				XmlNodeList xmlNodeList = this.GetXmlDoc().SelectNodes("Elements/Select");
				if (xmlNodeList.Count == 0)
				{
					this.ShowFirst(false);
				}
				else
				{
					this.ShowFirst(true);
					if (xmlNodeList.Item(0).Attributes["Check"].Value == "true")
					{
						this.chkFirst.Checked = true;
					}
					else
					{
						this.chkFirst.Checked = false;
					}
					this.txtFirst.Text = xmlNodeList.Item(0).Attributes["Name"].Value;
					if (xmlNodeList.Item(0).Attributes["Show"].Value == "true")
					{
						this.btnFirstShow.ImageIndex = 0;
					}
					else
					{
						this.btnFirstShow.ImageIndex = 1;
					}
					if (xmlNodeList.Item(0).Attributes["Lock"].Value == "true")
					{
						this.btnFirstLock.ImageIndex = 2;
					}
					else
					{
						this.btnFirstLock.ImageIndex = 3;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private XmlDocument GetXmlDoc()
		{
			XmlDocument xmlDocument = new XmlDocument();
			string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
			xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
			return xmlDocument;
		}

		private void btnDropDown_Click(object sender, EventArgs e)
		{
			this.frmCountSel.Close();
			this.frmCountSel = new FrmCountSel(this.cmdDataForm, this.msgForm, this.elementsForm);
			FrmCountSel frmCountSel = this.frmCountSel;
			frmCountSel.classSel = (FrmCountSel.delegateSel)Delegate.Combine(frmCountSel.classSel, new FrmCountSel.delegateSel(this.LoadFirst));
			this.frmCountSel.Owner = this;
			System.Drawing.Point point = base.PointToScreen(this.panel1.Location);
			this.frmCountSel.Top = point.Y;
			this.frmCountSel.Left = point.X;
			this.frmCountSel.Show();
		}

		private void btnDropDown_Enter(object sender, EventArgs e)
		{
			this.textBox1.Focus();
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void btnSelectElement_Click(object sender, EventArgs e)
		{
			bool visible = this.frmCountSel.Visible;
			bool visible2 = this.frmCountSpec.Visible;
			bool visible3 = this.frmCountMater.Visible;
			this.frmCountSel.Hide();
			this.frmCountSpec.Hide();
			this.frmCountMater.Hide();
			base.Hide();
			CmdTools.FrmMultiSelect(this.cmdDataForm, this);
			this.frmCountSel.Visible = visible;
			this.frmCountSpec.Visible = visible2;
			this.frmCountMater.Visible = visible3;
		}

		private void ChangeFirst(string attrName, string attrValue)
		{
			XmlDocument xmlDoc = this.GetXmlDoc();
			xmlDoc.SelectNodes("Elements/Select").Item(0).Attributes[attrName].Value = attrValue;
			string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
			XmlInfo.AddSelSet(pathName, title, xmlDoc.InnerXml);
		}

		private void chkFirst_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkFirst.Checked)
			{
				this.ChangeFirst("Check", "true");
				CmdTools.cmdSelect(this.cmdDataForm, this.txtFirst.Text);
				return;
			}
			this.ChangeFirst("Check", "false");
		}

		private void btnFirstShow_Click(object sender, EventArgs e)
		{
			if (this.btnFirstShow.ImageIndex == 0)
			{
				this.btnFirstShow.ImageIndex = 1;
				this.ChangeFirst("Show", "false");
				CmdTools.cmdShow(this.cmdDataForm, this.txtFirst.Text, false);
			}
			else
			{
				this.btnFirstShow.ImageIndex = 0;
				this.ChangeFirst("Show", "true");
				CmdTools.cmdShow(this.cmdDataForm, this.txtFirst.Text, true);
			}
			this.textBox1.Focus();
		}

		private void btnFirstLock_Click(object sender, EventArgs e)
		{
			if (this.btnFirstLock.ImageIndex == 2)
			{
				this.btnFirstLock.ImageIndex = 3;
				this.ChangeFirst("Lock", "false");
				CmdTools.cmdLock(this.cmdDataForm, this.txtFirst.Text, false);
			}
			else
			{
				this.btnFirstLock.ImageIndex = 2;
				this.ChangeFirst("Lock", "true");
				CmdTools.cmdLock(this.cmdDataForm, this.txtFirst.Text, true);
			}
			this.textBox1.Focus();
		}

		private void txtFirst_TextChanged(object sender, EventArgs e)
		{
			this.ChangeFirst("Name", this.txtFirst.Text);
		}

		private void btnDropSpec_Click(object sender, EventArgs e)
		{
			this.frmCountSpec.Close();
			this.frmCountSpec = new FrmCountSpec(this.cmdDataForm, this.msgForm, this.elementsForm);
			FrmCountSpec frmCountSpec = this.frmCountSpec;
			frmCountSpec.classSel = (FrmCountSpec.delegateSel)Delegate.Combine(frmCountSpec.classSel, new FrmCountSpec.delegateSel(this.BindFirstSpec));
			this.frmCountSpec.Owner = this;
			System.Drawing.Point point = base.PointToScreen(this.panel2.Location);
			this.frmCountSpec.Top = point.Y;
			this.frmCountSpec.Left = point.X;
			this.frmCountSpec.Show();
		}

		private void BindFirstSpec()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCountSpec);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Spec");
			if (xmlNodeList.Item(0).Attributes["Check"].Value == "true")
			{
				this.chkFirstSpec.Checked = true;
			}
			else
			{
				this.chkFirstSpec.Checked = false;
			}
			this.txtFirstSpec.Text = xmlNodeList.Item(0).Attributes["Name"].Value;
		}

		private void btnDropSpec_Enter(object sender, EventArgs e)
		{
			this.textBox1.Focus();
		}

		private void chkFirstSpec_CheckedChanged(object sender, EventArgs e)
		{
			XmlInfo.ChangeFirstSpec(this.chkFirstSpec.Checked);
		}

		private void panel2_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel2, e);
		}

		private void btnDropMater_Click(object sender, EventArgs e)
		{
			this.frmCountMater.Close();
			this.frmCountMater = new FrmCountMater();
			FrmCountMater frmCountMater = this.frmCountMater;
			frmCountMater.classSel = (FrmCountMater.delegateSel)Delegate.Combine(frmCountMater.classSel, new FrmCountMater.delegateSel(this.BindFirstMater));
			this.frmCountMater.Owner = this;
			System.Drawing.Point point = base.PointToScreen(this.panel3.Location);
			this.frmCountMater.Top = point.Y;
			this.frmCountMater.Left = point.X;
			this.frmCountMater.Show();
		}

		private void BindFirstMater()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCountMater);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Mater");
			if (xmlNodeList.Item(0).Attributes["Check"].Value == "true")
			{
				this.chkFirstMater.Checked = true;
			}
			else
			{
				this.chkFirstMater.Checked = false;
			}
			this.txtFirstMater.Text = xmlNodeList.Item(0).Attributes["Name"].Value;
		}

		private void btnDropMater_Enter(object sender, EventArgs e)
		{
			this.textBox1.Focus();
		}

		private void chkFirstMater_CheckedChanged(object sender, EventArgs e)
		{
			XmlInfo.ChangeFirstMater(this.chkFirstMater.Checked);
		}

		private void panel3_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel3, e);
		}

		private void btnCount_Click(object sender, EventArgs e)
		{
			CmdTools.CountGetResult(this.cmdDataForm);
			this.frmCountResult.Close();
			this.frmCountResult = new FrmCountResult();
			this.frmCountResult.Owner = this;
			this.frmCountResult.Show();
		}

		private void FrmCount_FormClosing(object sender, FormClosingEventArgs e)
		{
			XmlInfo.SetSelSetStr(this.sFileName, this.proPath, this.proTitle, XmlInfo.GetSelSet(this.proPath, this.proTitle));
			XmlInfo.DeleteSelSet(this.proPath, this.proTitle);
			MessageBox.Show("数据已经保存！");
		}

		private string proTitle = "";

		private string proPath = "";

		private string sPath = Assembly.GetExecutingAssembly().Location;

		private string sFileName = "";

		private ExternalCommandData cmdDataForm;

		private string msgForm;

		private ElementSet elementsForm = new ElementSet();

		private FrmCountSel frmCountSel = new FrmCountSel();

		private FrmCountSpec frmCountSpec = new FrmCountSpec();

		private FrmCountMater frmCountMater = new FrmCountMater();

		private FrmCountResult frmCountResult = new FrmCountResult();
	}
}
